package org.springcloud.wisdom.spring_01_canteen.indentdetail;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springcloud.wisdom.spring_01_canteen.common.util.RandomUtil;
import org.springcloud.wisdom.spring_01_canteen.dishes.cart.Cart;
import org.springcloud.wisdom.spring_01_canteen.dishes.cart.CartDao;
import org.springcloud.wisdom.spring_01_canteen.indent.Indent;
import org.springcloud.wisdom.spring_01_canteen.indent.IndentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IndentDetailServiceImpl implements IndentDetailService {
    @Autowired
    private IndentDetailDao indentDetailMapper;

    @Autowired
    private CartDao cartMapper;


    @Override
    public List<IndentDetail> listAll() {
        return indentDetailMapper.selectList(null);
    }

    @Transactional
    @Override
    public void importFromCart(String indentdetailid) {
        List<Cart> carts = cartMapper.selectList(null);

        for (Cart cart : carts) {
            IndentDetail indentDetail = new IndentDetail();
            indentDetail.setDname(cart.getDname());
            indentDetail.setPrice(cart.getPrice());
            indentDetail.setCount(cart.getCount());
            indentDetail.setIndentdetailid(indentdetailid);
            indentDetailMapper.insert(indentDetail);
        }

    }

    @Override
    public List<IndentDetail> listByIndentId(String indentdetailid) {
        return indentDetailMapper.selectList(new QueryWrapper<IndentDetail>().eq("indentdetailid", indentdetailid));
    }

}
